<template>
  <div class="main_box">
    <el-row>
      <el-col :span="12">
        <Crumbs></Crumbs>
      </el-col>
      <!-- 头部工具条 -->
      <el-col :span="12">
        <div class="opration_wrap" v-has="permissionAdd">
          <el-button type="success" icon="fa fa-plus" @click="handleClickAdd()">新增</el-button>
          <el-button type="success" icon="el-icon-refresh-right" @click="reload">刷新</el-button>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <div class="search_wrap">
        <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
          <el-form-item prop="specName">
            <el-input v-model="filters.specName" placeholder="规格名称"></el-input>
          </el-form-item>
          <el-form-item prop="institutionId">
            <el-select v-model="filters.institutionId" filterable placeholder="请选择代理医院">
              <el-option v-for="item in insData" :key="item.institutionId" :label="item.institutionName" :value="item.institutionId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-row>
    <el-row ref="tableRow">
      <el-table :data="tableList" :fit="true">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column width="200" align="center" prop="specId" label="规格ID" v-if="false"></el-table-column>
        <el-table-column width="200" align="center" prop="specName" label="规格名称"></el-table-column>
        <el-table-column width="300" align="center" prop="specValue" label="规格值"></el-table-column>
        <el-table-column width="200" align="center" prop="institutionName" label="医院名称"></el-table-column>
        <el-table-column width="140" align="center" prop="createUserName" label="创建人"></el-table-column>
        <el-table-column width="140" align="center" prop="createDate" label="创建时间"></el-table-column>
        <el-table-column width="200" align="center" prop="specNote" label="备注"></el-table-column>
        <el-table-column label="操作" min-width="180" fixed="right">
          <template slot-scope="scope">
            <el-row>
              <!--<el-button @click="handleClickDetail(scope.row)" size="mini">查看</el-button>-->
              <el-button v-has="permissionEdit" @click="handleClickEdit(scope.row)" size="mini">编辑</el-button>
              <el-button v-has="permissionDelete" @click="handleClickDelete(scope.row)" size="mini">删除</el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import Crumbs from '@/components/Crumbs';
import * as validators from '@/utils/validators';
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';
export default {
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      filters: {
        specName: '',
        institutionId: '',
      },
      tableList: [],
      insData: [],

      permissionAdd: 'SYSTEM_MANAGER_GOOD_SPEC_LIST_ADD',
      permissionEdit: 'SYSTEM_MANAGER_GOOD_SPEC_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_GOOD_SPEC_LIST_DELETE',
      page: {
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
        currentPage: 1,
      },
    };
  },
  components: {
    Crumbs,
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      let _this = this;
      _this.getTable();
      //_this.initDate();
      _this.getTableHeight();
      _this.getInsData();
    },

    resetForm() {
      this.$refs.searchForm.resetFields();
      this.page.pageNo = 1;
      this.onSearch();
      //this.initDate();
    },
    /*initDate(){
      let begin = new Date();
      begin.setHours(0);
      begin.setMinutes(0);
      begin.setSeconds(0);
      begin.setDate(1);
      let end = new Date();
      end.setHours(23);
      end.setMinutes(59);
      end.setSeconds(59);
      this.searchForm.date = [begin,end];
    },*/
    handleClickDetail(row) {
      this.$router.push({ path: '/good/goodsSpecDetail', query: { id: row.specId, institutionName: row.institutionName } });
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.getTable();
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.getTable();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.getTable();
    },
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/good/goodsSpecAdd');
    },
    handleClickEdit(row) {
      let _this = this;
      this.$router.push({ path: '/good/goodsSpecEdit', query: { id: row.specId } });
    },
    handleClickDelete(row) {
      this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        let deleteFormData = {
          specId: row.specId,
        };
        this.$Delete(this.$Config.api.goodsSpec, deleteFormData)
          .then((res) => {
            this.onSearch();
            this.$message({
              type: 'success',
              message: message.MSG_ACTION_SUCCESS,
            });
          })
          .catch((error) => {
            if (error.msg) {
              this.$message.error(message.MSG_ACTION_FAIL);
            }
          });
      });
    },
    getInsData() {
      this.$Get(this.$Config.api.institutionName)
        .then((res) => {
          this.insData = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    onSearch() {
      let searchData = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        specName: this.filters.specName,
        institutionId: this.filters.institutionId,
      };
      this.fetchData(searchData);
    },
    getTable() {
      let tableData = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
      };
      this.fetchData(tableData);
    },
    fetchData(data) {
      this.$emit('show-loading', true);
      this.$Get(this.$Config.api.goodsSpecList, data)
        .then((res) => {
          this.tableList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.page.currentPage = res.data.pageNum;
          this.$emit('show-loading', false);
        })
        .catch((error) => {
          console.error(error);
          this.$emit('show-loading', false);
        });
    },
    beforeRouteEnter(to, from, next) {
      next((vm) => {
        if (from.path == '/good/goodsSpecAdd' || from.path == '/good/goodsSpecEdit') {
          vm.init();
        }
      });
    },
  },
};
</script>
<style scope lang="scss" src="@/assets/css/views/content.scss"></style>
